package com.mr.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mr.model.BaseInfo;
import com.mr.model.Config;
import com.mr.model.Culture;
import com.mr.model.Discount;
import com.mr.model.Elegant;
import com.mr.model.Goods;
import com.mr.model.GoodsType;
import com.mr.model.Member;
import com.mr.model.News;
import com.mr.model.QQInfo;
import com.mr.model.Status;
import com.mr.model.Video;
import com.mr.util.DesUtils;
import com.mr.util.StringUtil;
import com.mr.web.support.PageUtil;

@Controller
public class PublicController extends AnnotationController{
	@RequestMapping("/auth/public/elegant.xhtml")
	public String elegant(HttpServletRequest req, ModelMap model){
		Elegant elegant = daoService.getObject(Elegant.class, Config.sysID);
		model.put("elegant", elegant);
		return "public/elegant.vm";
	}
	@RequestMapping("/auth/public/culture.xhtml")
	public String culture(HttpServletRequest req, ModelMap model){
		Culture culture = daoService.getObject(Culture.class, Config.sysID);
		model.put("culture", culture);
		return "public/culture.vm";
	}
	
	@RequestMapping("/auth/public/newsList.xhtml")
	public String newsList(Integer pageNo, ModelMap model){
		if(pageNo==null) pageNo = 0;
		Integer limit = 15;
		Integer count = commonService.getNewsCount(Config.sysID, null, Status.Y);
		List<News> newsList = commonService.getNewsList(Config.sysID, null, Status.Y, pageNo*limit, limit);
		
		Integer rowsPerPage = limit;
		PageUtil pageUtil = new PageUtil(count, rowsPerPage, pageNo, "auth/public/newsList.xhtml");
		pageUtil.initPageInfo();
		model.put("pageUtil", pageUtil);
		
		model.put("newsList", newsList);
		model.put("count", count);
		return "public/newsList.vm";
	}
	
	@RequestMapping("/auth/public/newsDetail.xhtml")
	public String newsDetail(Long id, ModelMap model){
		News news = daoService.getObject(News.class, id);
		model.put("news", news);
		return "public/newsDetail.vm";
	}
	@RequestMapping("/auth/public/discountList.xhtml")
	public String discountList(Integer pageNo, HttpServletRequest req, ModelMap model){
		if(pageNo==null) pageNo = 0;
		Integer limit = 15;
		Integer count = commonService.getDiscountCount(Config.sysID, null, null, null, Status.Y);
		List<Discount> discountList = commonService.getDiscountList(Config.sysID, null, null, null, Status.Y, pageNo*limit, limit);
		
		Integer rowsPerPage = limit;
		PageUtil pageUtil = new PageUtil(count, rowsPerPage, pageNo, "auth/public/discountList.xhtml");
		pageUtil.initPageInfo();
		model.put("pageUtil", pageUtil);
		
		model.put("count", count);
		model.put("discountList", discountList);
		
		return "public/discountList.vm";
	}
	
	@RequestMapping("/auth/public/discountDetail.xhtml")
	public String discountDetail(Long id, ModelMap model){
		Discount discount = daoService.getObject(Discount.class, id);
		model.put("discount", discount);
		return "public/discountDetail.vm";
	}
	@RequestMapping("/auth/public/videoList.xhtml")
	public String eduList(Integer pageNo, ModelMap model){
		if(pageNo==null) pageNo = 0;
		Integer limit = 15;
		Integer count = commonService.getVideoCount(null, null, Status.Y);
		List<Video> videoList = commonService.getVideoList(null, null, Status.Y, pageNo*limit, limit);
		
		Integer rowsPerPage = limit;
		PageUtil pageUtil = new PageUtil(count, rowsPerPage, pageNo, "auth/public/videoList.xhtml");
		pageUtil.initPageInfo();
		model.put("pageUtil", pageUtil);
		
		model.put("count", count);
		model.put("videoList", videoList);
		return "public/videoList.vm";
	}
	
	@RequestMapping("/auth/public/videoDetail.xhtml")
	public String eduDetail(Long id, ModelMap model){
		Video video = daoService.getObject(Video.class, id);
		model.put("video", video);
		return "public/videoDetail.vm";
	}
	
	@RequestMapping("/auth/public/goodsList.xhtml")
	public String goodsList(Integer pageNo, Long typeid, HttpServletRequest req, ModelMap model){
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		Member member = daoService.getObject(Member.class, base.getId());
		if(pageNo==null) pageNo = 0;
		Integer limit = 10;
		Integer count = commonService.getGoodsCount(member.getParentid(),typeid, null, Status.Y);
		List<Goods> goodsList = commonService.getGoodsList(member.getParentid(), typeid, null, Status.Y, pageNo*limit, limit);
		
		Integer rowsPerPage = limit;
		PageUtil pageUtil = new PageUtil(count, rowsPerPage, pageNo, "auth/public/goodsList.xhtml");
		pageUtil.initPageInfo();
		model.put("pageUtil", pageUtil);
		
		model.put("count", count);
		model.put("goodsList", goodsList);
		
		List<GoodsType> typeList = daoService.getAllObjects(GoodsType.class);
		model.put("typeList", typeList);
		
		return "public/goodsList.vm";
	}
	@RequestMapping("/auth/public/goodsDetail.xhtml")
	public String getGoods(Long id,  ModelMap model){
		Goods goods = daoService.getObject(Goods.class, id);
		model.put("goods", goods);
		return "public/goodsDetail.vm";
	}
	
	@RequestMapping("/auth/public/top.xhtml")
	public String top(ModelMap model){
		return "public/top.vm";
	}
	
	@RequestMapping("/auth/public/bottom.xhtml")
	public String bottom(ModelMap model){
		List<QQInfo> qqList = commonService.getQQInfoList(null);
		if(qqList.size()>10) qqList = qqList.subList(0, 10);
		model.put("qqList", qqList);
		return "public/bottom.vm";
	}
	
	@RequestMapping("/auth/public/savePwd.xhtml")
	public String modPwd(String oldpwd, String newpwd, HttpServletRequest req, ModelMap model){
		if(newpwd.length()<6) return showJsonFailure("密码长度至少为6位", model);
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		Long memberid = base.getId();
		Member member = daoService.getObject(Member.class, memberid);
		if(!StringUtils.equals(member.getRealpwd(), oldpwd)) return showJsonFailure("当前密码输入不正确", model);
		String password = StringUtil.md5(newpwd);
		String pwd = DesUtils.encrypt(newpwd);
		member.setPassword(password);
		member.setPwd(pwd);
		daoService.saveObject(member);
		return showJsonSuccess(model);
	}
	
	@RequestMapping("/auth/public/saveOrder.xhtml")
	public String saveOrder(Long goodsid, Integer quantity, HttpServletRequest req, ModelMap model){
		Goods goods = daoService.getObject(Goods.class, goodsid);
		if(goods.getRemain()<quantity) {
			return showJsonFailure("库存数量小于购买数量", model);
		}
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		goodsService.saveOrder(base, goods, quantity);
		return showJsonSuccess(model);
	}
}
